<template>
	<div class="login-wrap">
		<div class="ms-login">
			<div class="ms-title">MC管理系统</div>
			<!-- el-*  element组件库 -->
			<el-form :model="param" 
				:rules="rules" 
				ref="login" 
				label-width="0px" class="ms-content">
				<el-form-item prop="username">
					<el-input v-model="param.username" placeholder="用户名">
						<template #prepend>
							<el-button :icon="User"></el-button>
						</template>
					</el-input>
				</el-form-item>
				<el-form-item prop="password">
					<el-input
						type="password"
						placeholder="密码"
						v-model="param.password"
						@keyup.enter="submitForm(login)"
					>
						<template #prepend>
							<el-button :icon="Lock"></el-button>
						</template>
					</el-input>
				</el-form-item>
				<div class="login-btn">
					<el-button type="primary" @click="submitForm(login)">登录</el-button>
				</div>
				<p class="login-tips">Tips :。</p>
			</el-form>
		</div>
	</div>
</template>

<script setup lang="ts">
// 导入相关API
import { ref, reactive } from 'vue';
import { useTagsStore } from '../store/tags';
import { usePermissStore } from '../store/permiss';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import type { FormInstance, FormRules } from 'element-plus';
import { Lock, User } from '@element-plus/icons-vue';

// 导入api
import { loginFunc } from '../api/index'

// 编写接口规范对象
interface LoginInfo {
	username: string;
	password: string;
}
// 创建路由（用于跳转页面）
const router = useRouter();

// 记录用户信息
const param = reactive<LoginInfo>({
	username: '',
	password: ''
});

// 验证表单的规则
const rules: FormRules = {
	username: [
		{
			required: true,
			message: '请输入用户名',
			trigger: 'blur'
		}
	],
	password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
};

// 权限（路由守卫）
const permiss = usePermissStore();


// 定义对象的接口（对象的规范）
interface IContent {
	code: Number,
	nick_name: String,
	token: String,
	msg: String 
}
// 记录表单信息
const login = ref<FormInstance>();

// 提交表单信息
const submitForm = (formEl: FormInstance | undefined) => {
	// 如果表单没有值 就终止代码
	if (!formEl) return;
	// console.log(param);// 响应式对象
	// 调用管理员登录接口
	// admin_acc（账号   admin_pwd（密码）
	loginFunc({
		admin_acc: param.username,
		admin_pwd: param.password
	})
	.then(
		(content: IContent | any)=>{
			// console.log(content);
			let { code, msg, token, nick_name } = content;
			if(code == 200){
				ElMessage.success(msg);
				// 把管理信息储存在本地
				localStorage.setItem('ms_username', param.username);
				localStorage.setItem('nick_name', nick_name);
				localStorage.setItem('AUTO_TOKEN', token);
				// vue管理系统框架本身就有的
				const keys = permiss.defaultList[param.username == 'admin' ? 'admin' : 'user'];
				permiss.handleSet(keys);
			    localStorage.setItem('ms_keys', JSON.stringify(keys));

				// 跳转页面
				router.push('/');
			}
			else {
				ElMessage.error(msg);
			}
		}
	)

	// formEl.validate((valid: boolean) => {
	// 	if (valid) {
	// 		ElMessage.success('登录成功');
	// 		localStorage.setItem('ms_username', param.username);

	// 		// 假设登录成功 就缓存用户对应的key在本地
	// 		const keys = permiss.defaultList[param.username == 'admin' ? 'admin' : 'user'];
	// 		permiss.handleSet(keys);
	// 		localStorage.setItem('ms_keys', JSON.stringify(keys));

	// 		// 跳转页面
	// 		router.push('/');
	// 	} else {
	// 		ElMessage.error('登录失败');
	// 		return false;
	// 	}
	// });
};

const tags = useTagsStore();
tags.clearTags();
</script>

<style scoped>
.login-wrap {
	position: relative;
	width: 100%;
	height: 100%;
	/* background-image: url(../assets/img/login-bg.jpg); */
	background-size: 100%;
}
.ms-title {
	width: 100%;
	line-height: 50px;
	text-align: center;
	font-size: 20px;
	color: #fff;
	border-bottom: 1px solid #ddd;
}
.ms-login {
	position: absolute;
	left: 50%;
	top: 50%;
	width: 350px;
	margin: -190px 0 0 -175px;
	border-radius: 5px;
	background: rgba(255, 255, 255, 0.3);
	overflow: hidden;
}
.ms-content {
	padding: 30px 30px;
}
.login-btn {
	text-align: center;
}
.login-btn button {
	width: 100%;
	height: 36px;
	margin-bottom: 10px;
}
.login-tips {
	font-size: 12px;
	line-height: 30px;
	color: #fff;
}
</style>
